package com.tencent.mm.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import androidx.annotation.Nullable;

import com.tencent.mm.R;

public class MMSwitchBtn extends View {
    private int maxHeight;
    private int maxWidth;
    private int touchSlop;
    private Paint nNX = new Paint(1);
    private float initX;
    private float initY;
    private long initSysTime;
    private int wcb;
    private int wcc;
    private int height;
    private boolean animating = false;
    private boolean pointDown = false;
    private int padding;
    private int wch = 80;
    private int wci = 300;
    private float cornerRadius;
    private float someRadiusX;
    private int buttonColor;
    private int offBackgroundColor;
    private int onBackgroundColor;
    public boolean checked = false;
    private RectF backgroundRect = new RectF();
    private RectF buttonRect = new RectF();
    private ButtonAnimation animation = new ButtonAnimation();
    public OnCheckChangedListener wcs;

    public MMSwitchBtn(Context context) {
        this(context, null);
    }

    public MMSwitchBtn(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MMSwitchBtn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MMSwitchBtn);
        this.buttonColor = ta.getColor(R.styleable.MMSwitchBtn_sbButtonColor, Color.WHITE);
        this.offBackgroundColor = ta.getColor(R.styleable.MMSwitchBtn_sbUncheckedColor, Color.GRAY);
        this.onBackgroundColor = ta.getColor(R.styleable.MMSwitchBtn_sbCheckedColor, Color.GREEN);
        ta.recycle();

        this.touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.height = MeasureSpec.getSize(heightMeasureSpec);
        this.cornerRadius = height / 2F;
        this.padding = 8;
        this.someRadiusX = cornerRadius - padding;
    }

    @Override
    protected void onLayout(boolean z, int left, int top, int right, int bottom) {
        super.onLayout(z, left, top, right, bottom);
        this.maxWidth = right - left;
        this.maxHeight = bottom - top;
        this.wcc = (this.maxWidth - ((int) (2.0f * this.someRadiusX))) - (this.padding * 2);
        this.wcb = this.wcc / 2;
        if (this.height < this.maxHeight) {
            this.backgroundRect.top = (float) ((this.maxHeight - this.height) / 2);
            this.backgroundRect.bottom = this.backgroundRect.top + ((float) this.height);
        } else {
            this.backgroundRect.top = 0.0f;
            this.backgroundRect.bottom = (float) this.maxHeight;
        }
        this.backgroundRect.left = 0.0f;
        this.backgroundRect.right = (float) this.maxWidth;
        onCheckedChanged();
        this.nNX.setStyle(Paint.Style.FILL);
        this.nNX.setColor(this.offBackgroundColor);
    }

    private void onCheckedChanged() {
        if (this.height < this.maxHeight) {
            this.buttonRect.top = (float) (((this.maxHeight - this.height) / 2) + this.padding);
            this.buttonRect.bottom = (this.buttonRect.top + ((float) this.height)) - ((float) (this.padding * 2));
        } else {
            this.buttonRect.top = (float) this.padding;
            this.buttonRect.bottom = (float) (this.maxHeight - this.padding);
        }

        if (this.checked) {
            this.buttonRect.left = (float) (this.wcc + this.padding);
            this.buttonRect.right = (float) (this.maxWidth - this.padding);
        } else {
            this.buttonRect.left = (float) this.padding;
            this.buttonRect.right = (float) (((int) (2.0f * this.someRadiusX)) + this.padding);
        }
    }

    private void onButtonAnimating() {
        if (this.buttonRect.left < ((float) this.padding)) {
            this.buttonRect.left = (float) this.padding;
        }
        if (this.buttonRect.left > ((float) (this.wcc + this.padding))) {
            this.buttonRect.left = (float) (this.wcc + this.padding);
        }
        this.buttonRect.right = this.buttonRect.left + ((float) ((int) (2.0f * this.someRadiusX)));
    }


    private void cancelAction() {
        if (this.buttonRect.left > ((float) this.wcb)) {
            startAnimation(true);
            return;
        }
        startAnimation(false);
    }

    private void startAnimation(boolean checked) {
        this.animating = true;
        this.animation.reset();
        if (checked) {
            this.animation.wcu = (((long) this.wcc) - ((long) this.buttonRect.left)) + ((long) this.padding);
            this.animation.direction = 1;
        } else {
            this.animation.wcu = (long) this.buttonRect.left;
            this.animation.direction = 0;
        }
        this.animation.wct = this.buttonRect.left;
        this.animation.setDuration((((long) this.wch) * this.animation.wcu) / ((long) this.wcc));
        startAnimation(this.animation);
    }

    public final void setCheck(boolean check) {
        if (this.checked != check) {
            clearAnimation();
            this.checked = check;
            onCheckedChanged();
            this.animating = false;
            invalidate();
        }
        setContentDescription(check ? "Checked" : "Unchecked");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.nNX.setColor(this.offBackgroundColor);
        this.nNX.setAlpha(255/*WebView.NORMAL_MODE_ALPHA*/);
        canvas.drawRoundRect(this.backgroundRect, this.cornerRadius, this.cornerRadius, this.nNX);

        this.nNX.setColor(this.onBackgroundColor);
        this.nNX.setAlpha(Math.min(255/*WebView.NORMAL_MODE_ALPHA*/, (int) (255.0f * ((this.buttonRect.left - ((float) this.padding)) / ((float) this.wcc)))));
        canvas.drawRoundRect(this.backgroundRect, this.cornerRadius, this.cornerRadius, this.nNX);

        this.nNX.setColor(this.buttonColor);
        canvas.drawRoundRect(this.buttonRect, this.someRadiusX, this.someRadiusX, this.nNX);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z = false;
        if (!this.animating && isEnabled()) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    clearAnimation();
                    this.initX = motionEvent.getX();
                    this.initY = motionEvent.getY();
                    this.initSysTime = SystemClock.elapsedRealtime();
                    this.pointDown = false;
                    break;
                case MotionEvent.ACTION_UP:
                    if (SystemClock.elapsedRealtime() - this.initSysTime < ((long) this.wci)) {
                        startAnimation(!this.checked);
                    } else {
                        cancelAction();
                    }
                    disallowParentIntercept(false);
                    this.pointDown = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    float x;
                    if (this.pointDown) {
                        disallowParentIntercept(true);
                        x = motionEvent.getX() - this.initX;
                        RectF rectF = this.buttonRect;
                        rectF.left = x + rectF.left;
                        onButtonAnimating();
                    } else {
                        float x2 = motionEvent.getX() - this.initX;
                        x = motionEvent.getY() - this.initY;
                        if (Math.abs(x2) >= ((float) this.touchSlop) / 10.0f) {
                            if (x == 0.0f) {
                                x = 1.0f;
                            }
                            if (Math.abs(x2 / x) > 3.0f) {
                                z = true;
                            }
                        }
                        if (z) {
                            this.pointDown = true;
                            disallowParentIntercept(true);
                        }
                    }
                    this.initX = motionEvent.getX();
                    this.initY = motionEvent.getY();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    if (this.pointDown) {
                        cancelAction();
                    }
                    disallowParentIntercept(false);
                    this.pointDown = false;
                    break;
            }
            if (this.pointDown) {
                invalidate();
            }
        }
        return true;
    }

    public interface OnCheckChangedListener {
        void onChanged(boolean check);
    }

    private final class ButtonAnimation extends Animation {
        int direction = 0;
        float wct = 0.0f;
        long wcu = 0;

        private ButtonAnimation() {
            setInterpolator(new AccelerateDecelerateInterpolator());
            setAnimationListener(new Animation.AnimationListener() {
                public final void onAnimationEnd(Animation animation) {
                    boolean z = false;
                    if (checked != (direction == 1)) {
                        if (direction == 1) {
                            z = true;
                        }
                        checked = z;
                        post(new Runnable() {
                            public final void run() {
                                if (wcs != null) {
                                    wcs.onChanged(checked);
                                }
                            }
                        });
                    }
                    animating = false;
                }

                public final void onAnimationRepeat(Animation animation) {
                }

                public final void onAnimationStart(Animation animation) {
                }
            });
        }

        @Override
        protected final void applyTransformation(float f, Transformation transformation) {
            if (this.direction == 0) {
                buttonRect.left = this.wct - (((float) this.wcu) * f);
            } else {
                buttonRect.left = this.wct + (((float) this.wcu) * f);
            }
            onButtonAnimating();
            invalidate();
        }
    }

    private void disallowParentIntercept(boolean allow) {
        ViewParent parent = getParent();
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(allow);
        }
    }
}
